package com.crack.nga2.activity;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.crack.nga2.R;
import com.crack.nga2.fragment.EmotionFragment;
import com.crack.nga2.fragment.PostImageFragment;
import com.crack.nga2.volley.PostRequest;


import java.util.HashMap;
import java.util.Map;

public class PostActivity extends AppCompatActivity {
    private EditText etTitle, etContent;
    private String action;
    private String post_content;
    private String post_subject;
    //预置内容
    private String pre_content;
    private int pid;
    private int fid;
    private int tid;
    private RequestQueue mQueue;
    private FragmentManager fragmentManager;
    private PostImageFragment postImageFragment;
    private EmotionFragment emotionFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        etContent = (EditText) findViewById(R.id.edit_content);
        etTitle = (EditText) findViewById(R.id.edit_title);
        mQueue = NgaApplication.getQueue();
        Intent intent = getIntent();
        action = intent.getStringExtra("action");
        pre_content = intent.getStringExtra("content");
        if (!TextUtils.isEmpty(pre_content)) {
            etContent.setText(pre_content);
        }
        pid = intent.getIntExtra("pid", 0);
        fid = intent.getIntExtra("fid", 0);
        tid = intent.getIntExtra("tid", 0);
        fragmentManager = getSupportFragmentManager();

    }


    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.action_return:
                finish();
                break;
            case R.id.action_post:
                post_content = etContent.getText().toString();
                post_subject = etTitle.getText().toString();
//                if(TextUtils.isEmpty(post_subject)){
//                    etTitle.setError("标题不能为空");
//                    return;
//                }
                if (TextUtils.isEmpty(post_content)) {
                    etContent.setError("内容不能为空");
                }
                post();
                break;
            case R.id.btn_pic:
                if (postImageFragment == null) {
                    postImageFragment = new PostImageFragment();
                }
                fragmentManager.beginTransaction().replace(R.id.container, postImageFragment).commit();
                break;
            case R.id.btn_emotion:
                if (emotionFragment == null) {
                    emotionFragment = new EmotionFragment();
                }
                fragmentManager.beginTransaction().replace(R.id.container, emotionFragment).commit();
                break;
            case R.id.post_bold:
                break;
            case R.id.post_delete_line:
                break;
            case R.id.anonymous:
                break;
        }
    }


    private void post() {
        if (action == null) {
            Toast.makeText(this, "异常", Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("step", String.valueOf(2));
        params.put("lite", "js");
        params.put("action", action);
        if (post_content != null) {
            params.put("post_content", post_content);
        }
        if (post_subject != null) {
            params.put("post_subject", post_subject);
        }
        if (pid != 0) {
            params.put("pid", String.valueOf(pid));
        }
        if (tid != 0) {
            params.put("tid", String.valueOf(tid));
        }
        if (fid != 0) {
            params.put("fid", String.valueOf(fid));
        }
        mQueue.add(new PostRequest(params,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i("post", response);
                        Toast.makeText(PostActivity.this,"回帖成功",Toast.LENGTH_SHORT).show();
                        finish();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i("tag",error.getMessage());
                    }
                }));
    }


}
